import { observable, action } from 'mobx';
import { Bubble, Tools } from 'weaver-mobile';
import { Form, RouteLayout } from 'weaver-mobile-page';

import Api, { getMailLabel } from '../../apis/setting';
import TableListStore from './tableListStore';

const { getLabel } = Tools;
const initState = {
  condition: [],
  labelId: '',
  tableListParams: {
    url: getMailLabel,
    params: { },
  },
};

export default class LabelManageStore {
  @observable layout;
  @observable form;
  @observable tableListStore = new TableListStore(initState.tableListParams);
  @observable condition = initState.condition;
  @observable labelId = initState.labelId;

  @action
  handleOperation = (params, cb) => {
    Api.operateUserMailLabel(params).then(res => {
      const { flag, message } = res;
      if (flag) {
        this.tableListStore.refreshTableList();
        switch (params.operation) {
          case "add":
            Bubble.success(getLabel(83880, '添加成功！'))
            break;
          case 'clear':
            Bubble.success(getLabel(384426, '清空成功'))
            break;
          case 'del':
            Bubble.success(getLabel(83472, '删除成功！'))
            break;     
          default:
            Bubble.success(getLabel('83885','操作成功！'))
            break;
        }
        if (cb) cb(res);
      } else {
        if(params.operation == 'del'){
          if(res.msg){
            Bubble.error(res.msg, 1, { top: '50%' });
          }else{
            Bubble.error(getLabel(83473, '删除失败！'), 1, { top: '50%' });
          }
         
        }else{
          Bubble.error(message.replace(/repeat/, getLabel('519097','名称重复，操作失败')), 1, { top: '50%' });
        }

      }
    });
  }
  init = (path='/setting/labelManage') => {
    this.layout = new RouteLayout.Store({ id: 'addMailLabel', route: path });
    this.form = new Form.Store({ id: 'form', route: this.layout.path });
  }

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
    return this;
  }

  @action
  clear = () => this.setState(initState)
}
